var express = require('express');
var router = express.Router();

var Pet = require('../schema/PetsSchema'); //引入对象


// 获取自己的宠物信息, 通过手机号
router.post('/getpet', function (req, res) {
  if (req.body.token !== "12345") {
    res.send({ status: 2 });
    res.end();
    return;
  }
  Pet.find({ "phone": req.body.phone }, (err, doc) => {
    if (err) return console.error(err);
    if (doc.length === 0) {
      res.send({ status: 1 });
    } else {
      res.send({ status: 0, data: doc });
    }
    res.end();
  });
});
// 完善个人信息, 必须要token(值: 12345)
router.post('/uploadpet', function (req, res) {
  if (req.body.token !== "12345") {
    res.send({ status: 2 });
    res.end();
    return;
  }
  const pet = new Pet({
    "phone": req.body.phone,
    "nickname": req.body.nickname || null,
    "sex": req.body.sex || null,
    "breed": req.body.breed || null,
    "birth": req.body.birth || null,
    "gohome": req.body.gohome || null,
    "ster": req.body.ster || null,
    "reply": req.body.reply || null,
  });
  pet.save((err, doc) => {
    if (err) return console.error(err);
    if (doc.ok !== 0) {
      res.send({ status: 0 });
    } else {
      res.send({ status: 1 });
    }
    res.end();
  });
});



module.exports = router;
